package com.axonactive.yolo.dao.jpa.implement;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.axonactive.yolo.domain.Airline;
import com.axonactive.yolo.domain.Airline_;

public class AirlineSpecification {
	public static Specification<Airline> findFirstAirlineCharacterLike(
			final String searchTerm) {
		/**
		 * Creates a specification used to find persons whose last name begins
		 * with the given search term. This search is case insensitive.
		 * 
		 * @param searchTerm
		 * @return
		 */

		return new Specification<Airline>() {

			public Predicate toPredicate(Root<Airline> airlineRoot,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				StringBuilder pattern = new StringBuilder();
				pattern.append(searchTerm.toLowerCase() + "%");
				return cb.like(
						cb.lower(airlineRoot.<String> get(Airline_.name)),
						pattern.toString());
			}
		};
	}

	public static Specification<Airline> findAirlineTokenLike(
			final String searchTerm) {
		/**
		 * Creates a specification used to find persons whose last name begins
		 * with the given search term. This search is case insensitive.
		 * 
		 * @param searchTerm
		 * @return
		 */

		return new Specification<Airline>() {

			public Predicate toPredicate(Root<Airline> airlineRoot,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				StringBuilder pattern = new StringBuilder();
				pattern.append("%" + searchTerm.toLowerCase() + "%");
				return cb.like(
						cb.lower(airlineRoot.<String> get(Airline_.name)),
						pattern.toString());
			}
		};
	}
	
	public static Specification<Airline> findAirlineCodeLike(
			final String searchTerm) {

		return new Specification<Airline>() {

			public Predicate toPredicate(Root<Airline> airlineRoot,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				StringBuilder pattern = new StringBuilder();
				pattern.append(searchTerm);
				return cb.like(
						cb.lower(airlineRoot.<String> get(Airline_.id)),
						pattern.toString());
			}
		};
	}

}
